package com.idormy.sms.forwarder.receiver

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import com.idormy.sms.forwarder.activity.SplashActivity
import com.idormy.sms.forwarder.service.LauncherAccessibilityService
import com.idormy.sms.forwarder.utils.Log

/**
 * 快速启动接收器
 * 
 * 使用方法：
 * adb shell am broadcast -a com.idormy.sms.forwarder.QUICK_LAUNCH
 * 
 * 或者在其他应用中发送广播：
 * Intent intent = new Intent("com.idormy.sms.forwarder.QUICK_LAUNCH");
 * context.sendBroadcast(intent);
 */
class QuickLaunchReceiver : BroadcastReceiver() {

    companion object {
        private const val TAG = "QuickLaunchReceiver"
        const val ACTION_QUICK_LAUNCH = "com.idormy.sms.forwarder.QUICK_LAUNCH"
    }

    override fun onReceive(context: Context?, intent: Intent?) {
        if (context == null || intent == null) return
        
        if (intent.action != ACTION_QUICK_LAUNCH) return
        
        try {
            Log.i(TAG, "收到快速启动广播")
            
            // 方式1：如果无障碍服务已启用，通过服务启动
            if (LauncherAccessibilityService.isEnabled) {
                Log.i(TAG, "通过无障碍服务启动")
                LauncherAccessibilityService.launchApp()
            } else {
                // 方式2：直接启动
                Log.i(TAG, "直接启动应用")
                val launchIntent = Intent(context, SplashActivity::class.java).apply {
                    flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP
                }
                context.startActivity(launchIntent)
            }
            
            Log.i(TAG, "应用启动成功")
        } catch (e: Exception) {
            Log.e(TAG, "启动应用失败: ${e.message}")
            e.printStackTrace()
        }
    }
}
